    dojo.require('dojo.data.ItemFileReadStore');
    dojo.require('dojox.grid.DataGrid');
    dojo.require('dijit.form.Button');
    dojo.require('dijit.Dialog');
    dojo.require('dojo.parser');

	// URLs for grid datastores to get cases for a given FD
    var preregCasesBaseUrl = 'http://www.aldordemo.com/index/fdpreregcases/funeral_dir_id/';
    var pendingCasesBaseUrl = 'http://www.aldordemo.com/index/fdpendingcases/funeral_dir_id/';
    var finishedCasesBaseUrl = 'http://www.aldordemo.com/index/fdfinishedcases/funeral_dir_id/';

	var preregCasesURL = preregCasesBaseURL + '0';
	var pendingCasesURL = pendingCasesBaseURL + '0';
	var finishedCasesURL = finishedCasesBaseURL + '0';
	
    var preregCasesStore = new dojo.data.ItemFileReadStore({ url: preregCasesURL });
    var pendingCasesStore = new dojo.data.ItemFileReadStore({ url: pendingCasesURL });
    var finishedCasesStore = new dojo.data.ItemFileReadStore({ url: finishedCasesURL });

	// this returns a VAS case id
	function getSelectedPreregingCaseId()
	{
	}
	
	// these both return FDMS case#
	function getSelectedPendingCaseNum()
	{
		var items = funeralHomesGrid.selection.getSelected();
		if(items.length != 1) return -1;
	
		var selectedItem = items[0];
		var fhname = funeralHomesGrid.store.getValues(selectedItem, 'name');
	
		return fhname;
	}
	
	function getSelectedFinishedCaseNum()
	{
		var items = funeralHomesGrid.selection.getSelected();
		if(items.length != 1) return -1; 
	
		var selectedItem = items[0];
		var fhname = funeralHomesGrid.store.getValues(selectedItem, 'name');
	
		return fhname;
	}
	
//////////////////////////

// button click handler for Cases grid 'review' button

    function fdCaseReviewBtnClick( )
    {
		var case_id = getSelectedVASCaseId();
		var gotoURL = funeralCaseReviewQueryBaseUrl+case_id;
//		alert(gotoURL);
		window.location = gotoURL;
    }


    function caseGridRowClick(row)
    {
	// enables the Review button
    }

// serious Dojo begins here

    dojo.addOnLoad( function() {

	    funeralHomesGrid = new dojox.grid.DataGrid({
		query: { funeral_home_id: '*' },
		store: funeralHomeStore,
		structure: [
		    { field: "funeral_home_id", name: "ID", width:"80px" },
		    { field: "name", name: "Name", width:"auto" },
		    { field: "citystate", name: "City, State", width:"auto" }
		],
		rowsPerPage: 3,
		selectionMode: 'single'
		}, 'funeralHomesGrid');

	    funeralDirectorsGrid = new dojox.grid.DataGrid({
		query: { id: '*' },
		store: funeralDirectorStore,
		structure: [
		    { field: "id", name: "ID", width:"80px" },
		    { field: "name", name: "Name", width:"auto" }
		],
		rowsPerPage: 3,
		selectionMode: 'single'
		}, 'funeralDirectorsGrid');

	    funeralCasesGrid = new dojox.grid.DataGrid({
		query: { case_num: '*' },
		store: funeralCasesStore,
		structure: [
		    { field: "id", name: "VAS Case ID", width:"30px" },
		    { field: "case_num", name: "Case ID", width:"100px" },
		    { field: "status", name: "Status", width:"auto" },
		    { field: "name", name: "Name", width:"auto" },
		    { field: "dob", name: "Born", width:"auto" },
		    { field: "dod", name: "Died", width:"auto" },
		    { field: "residence", name: "Resided In", width:"auto" },
		],
		rowsPerPage: 3,
		selectionMode: 'single'
		}, 'funeralCasesGrid');

	    priorityMerchGrid = new dojox.grid.DataGrid({
		query: { id: '*' },
		store: priorityMerchStore,
		structure: [
		    { field: "rank", name: "Rank", width:"30px" },
		    { field: "region", name: "Region", width:"60px" },
		    { field: "FDMS_product_code", name: "FDMS SKU", width:"auto" },
		    { field: "product_description", name: "Description", width:"auto" },
		],
		rowsPerPage: 3,
		selectionMode: 'single'
		}, 'priorityMerchGrid');

	    cashAdvancesGrid = new dojox.grid.DataGrid({
		query: { id: '*' },
		store: cashAdvancesStore,
		structure: [
		    { field: "id", name: "CaID", width:"50px" },
		    { field: "region", name: "Region", width:"auto" },
		    { field: "area", name: "Area", width:"auto" },
		    { field: "name", name: "Descr", width:"200px" },
		    { field: "amt", name: "Amt", width:"auto" },
		],
		rowsPerPage: 3,
		selectionMode: 'single'
		}, 'cashAdvancesGrid');

		funeralHomesGrid.startup();
		funeralDirectorsGrid.startup();
		funeralCasesGrid.startup();
		priorityMerchGrid.startup();
		cashAdvancesGrid.startup();
	});
